<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          :preIcon="IconEnum.EXPORT"
          @click="downloadExcel(authExport, '授权用户数据')"
          v-auth="'auth:auth:export'"
          >数据</a-button
        >
        <!-- <a-button
          :preIcon="IconEnum.DELETE"
          type="error"
          @click="multipleRemove(authRemove)"
          :disabled="!selected"
          v-auth="'auth:auth:remove'"
          >删除</a-button
        >
        <a-button
          type="primary"
          :pre-icon="IconEnum.ADD"
          @click="handleAdd"
          v-auth="'auth:auth:add'"
          >新增</a-button
        > -->
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'headImg'">
          <avatar v-if="record.headImg" :src="record.headImg" />
          <avatar
            v-else
            src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
          />
        </template>
        <template
          v-if="
            column.key === 'action' &&
            (record.releaseStatus === 'NO' ||
              record.releaseStatus === 'NOT' ||
              record.releaseStatus === 'YES')
          "
        >
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '上传代码',
                icon: IconEnum.EXPORT,
                type: 'link',
                ghost: true,
                auth: 'auth:auth:edit',
                onClick: handleUploadCode.bind(null, record),
              },
            ]"
          />
        </template>
        <template v-if="column.key === 'action' && record.releaseStatus === 'CODE_COMMIT'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '提交审核',
                icon: IconEnum.UPLOAD,
                type: 'link',
                ghost: true,
                auth: 'auth:auth:edit',
                onClick: handleSubmitAudit.bind(null, record),
              },
            ]"
          />
        </template>
        <template v-if="column.key === 'action' && record.releaseStatus === 'CODE_SUBMIT_SUCCESS'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '发布小程序',
                icon: IconEnum.UPLOAD,
                type: 'link',
                ghost: true,
                auth: 'auth:auth:edit',
                onClick: handleRelease.bind(null, record),
              },
            ]"
          />
        </template>
        <template v-if="column.key === 'action' && record.releaseStatus === 'HANDLE'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '查询审核状态',
                icon: IconEnum.SYNC,
                type: 'link',
                ghost: true,
                auth: 'auth:auth:edit',
                onClick: handleQueryStatus.bind(null, record),
              },
            ]"
          />
        </template>
        <template v-if="column.key === 'action' && record.releaseStatus === 'HANDLE'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '撤回审核',
                icon: IconEnum.CLOSE,
                type: 'link',
                ghost: true,
                auth: 'auth:auth:edit',
                onClick: handleRevertCodeRelease.bind(null, record),
              },
            ]"
          />
        </template>
        <template v-if="column.key === 'action' && record.releaseStatus == 'YES'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '回退上个版本',
                icon: IconEnum.REFRESH,
                type: 'link',
                ghost: true,
                auth: 'auth:auth:edit',
                onClick: handleRevertCodeRelease.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AuthModal @register="registerModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { Avatar } from 'ant-design-vue';
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import {
    authList,
    authExport,
    submitAudit,
    uploadCode,
    queryStatus,
    revertCodeRelease,
    release,
  } from '@/api/ma/auth';
  import { downloadExcel } from '@/utils/file/download';
  import { useModal } from '@/components/Modal';
  import AuthModal from './AuthModal.vue';
  import { formSchems, columns } from './auth.data';
  import { IconEnum } from '@/enums/appEnum';

  defineOptions({ name: 'Auth' });

  const [registerTable, { reload }] = useTable({
    rowSelection: {
      type: 'checkbox',
    },
    title: '授权用户列表',
    api: authList,
    showIndexColumn: false,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchems,
      baseColProps: {
        span: 8,
      },
    },
    columns: columns,
    actionColumn: {
      width: 200,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });

  const [registerModal] = useModal();

  async function handleSubmitAudit(record: Recordable) {
    await submitAudit([record.id]);
    await reload();
  }
  async function handleUploadCode(record: Recordable) {
    await uploadCode([record.id]);
    await reload();
  }
  async function handleQueryStatus(record: Recordable) {
    await queryStatus([record.id]);
    await reload();
  }
  async function handleRevertCodeRelease(record: Recordable) {
    await revertCodeRelease([record.id]);
    await reload();
  }
  async function handleRelease(record: Recordable) {
    await release([record.id]);
    await reload();
  }
</script>

<style scoped></style>
